package com.wgl.cmn.controller;

import com.wgl.cmn.service.DictService;
import com.wgl.model.dict.Dict;
import com.wgl.yygh.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author wgl
 * @version 1.0.0
 * @ClassName DictController.java
 * @Description TODO
 * @createTime 2022年02月04日 22:27:00
 */
@Api(value = "数据字典")
@RestController
//@CrossOrigin
@RequestMapping("/admin/cmn/dict")
public class DictController {
    @Resource
    private DictService dictService;


    @ApiOperation(value = "根据dictCode获取下级节点")
    @GetMapping("/findByDictCode/{dictcode}")
    public Result findByDictCode(@PathVariable String dictcode){
        return Result.ok(dictService.getdDictByDictCode(dictcode));
    }

    @ApiOperation(value = "导出数据字典")
    @GetMapping("/exportData")
    public Result exportData(HttpServletResponse httpServletResponse){
            dictService.exportData(httpServletResponse);
            return Result.ok();
    }
    @ApiOperation(value = "导入数据字典")
    @PostMapping("/importData")
    public Result importData(MultipartFile file){
            dictService.importData(file);
            return Result.ok();
    }

    @ApiOperation(value = "根据id获得其子数据列表")
    @GetMapping("/findChildData/{id}")
    public Result findChildData(@PathVariable Long id){
        List<Dict> list = dictService.findChildData(id);
        return Result.ok(list);
    }
    @ApiOperation(value = "根据dictCode和value获得Name")
    @GetMapping("/getName/{dictCode}/{value}")
    public Result getName(@PathVariable String dictCode,@PathVariable String value){
        String name = dictService.getDictCodeName(dictCode,value);
        return Result.ok(name);
    }
    @ApiOperation(value = "根据value获得Name")
    @GetMapping("/getName/{value}")
    public Result getName(@PathVariable String value){
        String name = dictService.getDictCodeName(null,value);
        return Result.ok(name);
    }
}
